#include <glut.h>

float angle = 0.0;
float delta = 1.0;
GLfloat zAngle = -5.0;
GLfloat xAngle = 0.0;

void Timer(int extra)
{
	glutPostRedisplay();
	glutTimerFunc(0.0001,Timer,0);
}

void keyboard(unsigned char key, int x, int y)
{
    switch(key)//Get a character via keyboard buffer
    {
    case 'W':case'w': // Forward
		zAngle += 0.1;
        break;
	case 'S':case's': // Backwards
		zAngle -= 0.1;
        break;
	case 'A':case'a': // Left
		xAngle -= 0.1;
		break;
	case 'D':case'd': // Right
		xAngle += 0.1;
		break;
    default:
        break;//Don't do anything if any other keys are pressed
    }
}

void Initialize(){
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClearDepth(1.0);
	glShadeModel(GL_SMOOTH);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f, 1.0f, 1.0f, 100.f);
}

void renderScene()	{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();

	//Cube
	glPushMatrix();
	glTranslatef(xAngle, 0.0f, zAngle);
	glRotatef(angle, 0.0f, 1.0f, 0.0f);

		glBegin(GL_QUADS);
		//Front
		glColor3f(1.0, 1.0, 0.0);
		glVertex3f(-0.5, 1.0, 0.0);
		glVertex3f(0.5, 1.0, 0.0);
		glVertex3f(0.5, 0.0, 0.0);
		glVertex3f(-0.5, 0.0, 0.0);
		//Left
		glColor3f(0.0, 0.0, 1.0);
		glVertex3f(-0.5, 1.0, 0.0);
		glVertex3f(-0.5, 1.0, -1.0);
		glVertex3f(-0.5, 0.0, -1.0);
		glVertex3f(-0.5, 0.0, 0.0);
		//Right
		glColor3f(0.0, 1.0, 0.0);
		glVertex3f(0.5, 1.0, 0.0);
		glVertex3f(0.5, 1.0, -1.0);
		glVertex3f(0.5, 0.0, -1.0);
		glVertex3f(0.5, 0.0, 0.0);
		//Back
		glColor3f(1.0, 0.0, -1.0);
		glVertex3f(-0.5, 1.0, -1.0);
		glVertex3f(-0.5, 0.0, -1.0);
		glVertex3f(0.5, 0.0, -1.0);
		glVertex3f(0.5, 1.0, -1.0);
		//Top
		glColor3f(1.0, 0.0, 1.0);
		glVertex3f(0.5, 1.0, 0.0);
		glVertex3f(0.5, 1.0, -1.0);
		glVertex3f(-0.5, 1.0, -1.0);
		glVertex3f(-0.5, 1.0, 0.0);
		//Bottom
		glColor3f(0.0, 1.0, 1.0);
		glVertex3f(0.5, 0.0, 0.0);
		glVertex3f(0.5, 0.0, -1.0);
		glVertex3f(-0.5, 0.0, -1.0);
		glVertex3f(-0.5, 0.0, 0.0);
	glEnd();
	glPopMatrix();

	//Triangle-based pyramid
	/*
	glPushMatrix();
	glTranslatef(0.0, 0.0, -5.0);
	glRotatef(-angle, 0.5f, 1.0f, 0.0f);

	glBegin(GL_TRIANGLES);
		// Front triangle
		glColor3f(0.0, 1.0, 1.0);
		glVertex3f(-0.5, -1.0, 0.0); // Bottom left corner
		glColor3f(0.0, 0.0, 1.0);
		glVertex3f(0.5, -1.0, 0.0); // Bottom right 
		glColor3f(1.0, 0.0, 0.0);
		glVertex3f(0.0, 1.0, -0.5); // Top corner

		// Right Triangle
		glColor3f(0.0, 1.0, 0.0);
		glVertex3f(0.5, -1.0, 0.0); // Bottom left corner
		glVertex3f(0.0, -1.0, -1.0); // Bottom right corner
		glVertex3f(0.0, 1.0, -0.5); // Top corner

		// Left Triangle
		glColor3f(1.0, 1.0, 0.0);
		glVertex3f(-0.5, -1.0, 0.0); // Bottom right corner
		glVertex3f(0.0, -1.0, -1.0); // Bottom left corner
		glVertex3f(0.0, 1.0, -0.5); // Top corner

		// Bottom triangle (all comments in top-down view)
		glColor3f(1.0, 0.0, 0.0);
		glVertex3f(-0.5, -1.0, 0.0); // Bottom left corner
		glVertex3f(0.5, -1.0, 0.0); // Bottom right corner
		glVertex3f(0.0, -1.0, -1.0); // Top corner
	glEnd();*/

	//Cube
	/*
	glTranslatef(0.0, 0.0, -5.0);
	glRotatef(-angle, 0.0f, 3.0f, 0.0f);

	glColor3f(0.0, 1.0, 0.0);
	glTranslatef(0.f, 0.f, 0.f);
	glutSolidSphere(0.75, 20, 20);
	*/
	/*
	glBegin(GL_LINES);
		//Z-axis
		glColor3f(1.0, 0.0, 0.0);
		glVertex3f(0.0, 0.0, -1.0);
		glVertex3f(0.0, 0.0, 1.0);
		//X-axis
		glColor3f(1.0, 1.0, 0.0);
		glVertex3f(-1.0, 0.0, 0.0);
		glVertex3f(1.0, 0.0, 0.0);
		//Y-axis
		glColor3f(0.0, 0.0, 1.0);
		glVertex3f(0.0, -1.0, 0.0);
		glVertex3f(0.0, 1.0, 0.0);
	glEnd();
	glPopMatrix();*/

	glutSwapBuffers();
	glLoadIdentity();

	if (angle >= 360)	{
		angle = 0.0;
	}
}

int main(int argc, char** argv)	{
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE | GLUT_DEPTH);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(100, 100);
	glutCreateWindow("GLUTWindow");
	Initialize();
	glutDisplayFunc(renderScene);
	glutKeyboardFunc(keyboard);
	glutTimerFunc(0,Timer,0);
	glutMainLoop();
	return 0;
}

/*
#include "glut.h"

int GAngle=0;

void Display(void)
	{
	glClear(GL_COLOR_BUFFER_BIT);

	glLoadIdentity();
	glRotated(GAngle,0,1,0);

	glBegin(GL_TRIANGLES);
		glVertex3f(-1,0,0);
		glVertex3f(1,0,0);
		glVertex3f(0,1,0);
	glEnd();

	GAngle = GAngle + 1;

	glFlush();

	glutSwapBuffers();
	}

void Timer(int extra)
	{
	glutPostRedisplay();
	glutTimerFunc(30,Timer,0);
	}

int main(void)
	{
	// Init to double buffering
	glutInitDisplayMode(GLUT_DOUBLE|GLUT_RGB);
	glutCreateWindow("This is the window title");
	glutDisplayFunc(Display);
	glutTimerFunc(0,Timer,0);
	
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glFrustum(-1,1,-1,1,1,3);
	glTranslated(0,0,-2);
	glMatrixMode(GL_MODELVIEW);

	glutMainLoop();
	return 0;
	}*/


//################################################################

/*#include <glut.h>

float angle = 0.0;
float delta = 0.1;

void renderScene(void){
	glMatrixMode(GL_MODELVIEW);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	glTranslatef(0.0, 0.0, -4.375f);
	glRotatef(angle, 0.1, 1.0, 0.0);

	glBegin(GL_TRIANGLES);
		// Front triangle
		glColor3f(0.0, 1.0, 1.0);
		glVertex3f(-0.375, -1.0, 0.0); // Bottom left corner
		glVertex3f(0.375, -1.0, 0.0); // Bottom right corner
		glVertex3f(0.0, 1.0, -0.375); // Top corner

		// Right Triangle
		glColor3f(0.0, 1.0, 0.0);
		glVertex3f(0.375, -1.0, 0.0); // Bottom left corner
		glVertex3f(0.0, -1.0, -0.75); // Bottom right corner
		glVertex3f(0.0, 1.0, -0.375); // Top corner

		// Left Triangle
		glColor3f(1.0, 1.0, 0.0);
		glVertex3f(-0.375, -1.0, 0.0); // Bottom right corner
		glVertex3f(0.0, -1.0, -0.75); // Bottom left corner
		glVertex3f(0.0, 1.0, -0.375); // Top corner

		// Bottom triangle (all comments in top-down view)
		glColor3f(1.0, 0.0, 0.0);
		glVertex3f(-0.375, -1.0, 0.0); // Bottom left corner
		glVertex3f(0.375, -1.0, 0.0); // Bottom right corner
		glVertex3f(0.0, -1.0, -0.75); // Top corner
	glEnd();

	glLoadIdentity();

	glFlush();

	angle += delta;

	if (angle >= 360){
		angle = 0.0;
	}

	glutPostRedisplay();
}

void Initialize(){
	glShadeModel(GL_SMOOTH);
	glClearColor(0.0, 0.0, 0.0, 0.0);
	glClearDepth(1.0);
	glEnable(GL_DEPTH_TEST);
	glDepthFunc(GL_LEQUAL);
	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45.0f, 1.0, 1.f, 100.f);
}

int main(int argc, char** argv){
	glutInit(&argc, argv);
	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
	glutInitWindowSize(500, 500);
	glutInitWindowPosition(200, 200);
	glutCreateWindow("Window :D");
	Initialize();
	glutDisplayFunc(renderScene);
	glutMainLoop();
	return 0;
}*/